//
//  CheckView.m
//  JiuLe
//
//  Created by 黄鑫 on 2016/10/27.
//  Copyright © 2016年 Fine. All rights reserved.
//

#import "CheckView.h"

@implementation CheckView
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if(self) {
        [self initView];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView{
    self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.viewMain = [self viewWithTag:1];
    self.viewMain.layer.cornerRadius = 8;
    self.viewMain.layer.masksToBounds = YES;
    viewMainRect = self.viewMain.frame;
    self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin;
    UITapGestureRecognizer *tapGesturRecognizer= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)];
    [self addGestureRecognizer:tapGesturRecognizer];
    self.show = NO;
}

#pragma mark - IBAction
- (IBAction)helpAction:(id)sender {
    NSLog(@"Help");
    [self.delegate helpAction];
}

- (IBAction)IntegralAction:(id)sender {
    _show = NO;
    UIButton *btn = (UIButton *)sender;
    if ([btn.titleLabel.text isEqualToString:@"点击领取今日积分"]) {
        [self.delegate checkAction];
    }else {
        if ([[_dictModel objectForKey:@"iswear"] isEqualToString:@"no"]) {
            [self.delegate remonvView:self with:@""];
        }else{
            [self.delegate remonvView:self with:@""];
        }
    }
}

#pragma mark - 私有方法
- (void)initData:(NSDictionary *)dict{
    _dictModel = dict;
    _btnIntegral.enabled = YES;
    _labOneIntegral.text = [NSString stringWithFormat:@"+%@",[dict objectForKey:@"score"][0]];
    _labTwoIntegral.text = [NSString stringWithFormat:@"+%@",[dict objectForKey:@"score"][1]];
    _labIntegral.text  = [NSString stringWithFormat:@"+%@",[dict objectForKey:@"score"][2]];
    _labSIntegral.text = [NSString stringWithFormat:@"+%@",[dict objectForKey:@"score"][3]];
    _labFourIntegral.text  = [NSString stringWithFormat:@"+%@",[dict objectForKey:@"score"][4]];
    if ([[_dictModel objectForKey:@"iswear"] isEqualToString:@"no"]) {
        _labCheckDay.text = @"昨日未佩戴久乐表，不能获取积分";
        [_btnIntegral setTitle:@"暂不能获取积分" forState:UIControlStateNormal];
    }else{
        _labCheckDay.text = [NSString stringWithFormat:@"您已连续签到%@天",[dict objectForKey:@"myday"]];
        if ([[dict objectForKey:@"issign"] isEqualToString:@"no"]) {//未签到
            [_btnIntegral setTitle:@"点击领取今日积分" forState:UIControlStateNormal];
        }else{
            [_btnIntegral setTitle:@"已签到" forState:UIControlStateNormal];
            _labTitle.text = @"今日已领";
        }
    }

}

- (void)requestData{
    _show = YES;
    //_btnIntegral.enabled = NO;
    _activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(self.center.x-20, self.center.y-95, 40, 40)];
    _activity.hidesWhenStopped = YES;
    _activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [_activity startAnimating];
    [self addSubview:_activity];
    //动画
    self.viewMain.alpha = 0.0;
    self.viewMain.frame = CGRectMake(CGRectGetMinX(viewMainRect), 0, viewMainRect.size.width, viewMainRect.size.height);
    [UIView animateWithDuration:0.7 animations:^{
        self.viewMain.alpha = 1.0;
        self.viewMain.frame = viewMainRect;
    } completion:nil];
}

- (void)requestEnd{
    [_activity stopAnimating];
    _activity.hidden = YES;
}

- (void)tapView:(UITapGestureRecognizer *)recognizer{
    CGPoint point = [recognizer locationInView:self];
    UIView *viewBG = [self viewWithTag:1];
    NSLog(@"%f",CGRectGetMinY(viewBG.frame));
    if (point.y < CGRectGetMinY(viewBG.frame) || point.y > CGRectGetMaxY(viewBG.frame)) {
        self.show = NO;
        [self.delegate remonvView:self with:@""];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
